package com.servlet;

import com.bean.UpdatePws;
import com.bean.User;
import com.dao.UserDao;
import com.utils.MD5Util;
import com.utils.PageUtil;
import com.utils.ResponseUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName UserServlet
 * @Desc 前台用户控制层
 * @Author xiaoding
 * @Date 2021/1/16 20:10
 * @Version 1.0
 */
@WebServlet(name = "UserServlet",value = "/UserServlet")
public class UserServlet extends HttpServlet {
    private UserDao userDao = new UserDao();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数，判断功能模块
        String str = request.getParameter("flag");

        //判断功能模块
        if (str.equals("update")) {
            //获取参数
            int uId = Integer.parseInt(request.getParameter("uId"));
            String account = request.getParameter("account");
            String pws = MD5Util.MD5(request.getParameter("pws"));
            String uName =  request.getParameter("uName");
            String email = request.getParameter("email");
            int count = Integer.parseInt(request.getParameter("count"));
            int age = Integer.parseInt(request.getParameter("age"));
            String sex = request.getParameter("sex");
            //设置状态、路径
            String message = null;
            String url = null;

            //调用修改信息的方法
            int a = userDao.updateUser(new User(uId,account,pws,uName,email,count,age,sex));

            //判断是否修改成功
            if (a > 0) {
                message = "内容修改成功";
                url = "BookServlet?flag=select";
            }else {
                message = "内容修改失败";
                url = "BookServlet?flag=select";
            }

            //获取用户信息
            User user = userDao.userSelectId(uId);
            //设置加密前的密码
            user.setPws(request.getParameter("pws"));
            //设置会话
            request.getSession().setAttribute("user",user);

            //跳转页面
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("updatePws")) {
            //获取参数
            int uId = Integer.parseInt(request.getParameter("uId"));
            String oldPws = MD5Util.MD5(request.getParameter("oldPws"));
            String newPws = MD5Util.MD5(request.getParameter("newPws"));
            //设置状态、路径
            String message = null;
            String url = null;

            //调用修改密码的方法
            int a = userDao.updatePws(new UpdatePws(uId,oldPws,newPws));

            //判断是否修改成功
            if (a > 0) {
                message = "密码修改成功";
                url = "LoginServlet?flag=exit";
            }else {
                message = "密码修改失败";
                url = "BookServlet?flag=select";
            }
            //跳转页面
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("isRepetitive")) {
            //获取数据
            List<User> list = userDao.isRepetitive();
            //将数据转换为JSON格式
            JSONObject obj = new JSONObject();
            obj.put("data", JSONArray.fromObject(list));
            //发送数据
            ResponseUtils.writeHTML(response,obj.toString());
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
